////////////////////////////////////////////////////////////////////////////////


*This do-file creates all figures in the main text of the paper


////////////////////////////////////////////////////////////////////////////////


////////////////////////////////////////////////////////////////////////////////
*Figure 1
////////////////////////////////////////////////////////////////////////////////

cd "$otherdata"

use "clean/nlp.dta", clear
gen rising=(prediction==1)
gen falling=(prediction==2)
gen claim=(prediction==1|prediction==2)

collapse (sum) rising falling claim, by(year)
tempfile temp
save `temp'

import delimited "raw/scopus_allpubs.csv", rowrange(9) clear
rename (scopus10956208analyzeyear v2) (year total)
merge 1:1 year using `temp'

drop if year<1979
drop if year>=2021

gen proportion=10000*claim/total

egen help=ma(proportion)
sum proportion if year==1981
gen scaled_proportion=100*help/`r(mean)'
drop help
egen help=ma(total)
sum total if year==1980
gen scaled_total=100*help/`r(mean)'
drop help
gen help=rising/claim
recode help .=0
egen proportion_rising=ma(help)
drop help

graph twoway (scatter scaled_total year, c(l) msize(0) lcolor(gs11) lpattern(dash) lwidth(medthick) yaxis(1)) (scatter scaled_proportion year, c(l) msize(0) lcolor(gs7) lpattern(longdash) lwidth(medthick) yaxis(1)) (scatter proportion_rising year, c(l) msize(0) lcolor(gs0) lwidth(medthick) yaxis(2)), xlabel(1980(3)2020, angle(45)) ylabel(100(250)1200, axis(1)) ylabel(0(.25)1, axis(2)) graphregion(color(white)) bgcolor(white) legend(label(1 "Gross job security" "publications") label(2 "Papers per social science" "publication") label(3 "Proportion that claim" "insecurity is rising")) ytitle("Index", axis(1)) ytitle("Proportion", axis(2)) xtitle("Year") note("1980=100. Underlying data are 3-year moving averages.")

graph export "$othergraphics/nlp.png", as(png) replace

////////////////////////////////////////////////////////////////////////////////
*Figure 2
////////////////////////////////////////////////////////////////////////////////

cd "$usdata"

use "clean/hrs_clean.dta", clear
	collapse (mean) insecurity if insample [pw=wtresp], by(year)
	rename insecurity insecurity_hrs
tempfile hrs
save `hrs'

use "clean/gss_clean.dta", clear
	
collapse (mean) insecure if insample [pweight=wtssall], by(year)

*Add unemployment data
merge 1:1 year using "clean/unrate.dta"
	drop if _merge==2
	drop _merge

*Merge in labor flow rates
merge 1:1 year using "clean/us_flows.dta"
	drop _merge
	sort year
	drop if year>2018|year<1978
	
*Merge in HRS data
merge 1:1 year using `hrs'
	drop _merge

replace cps_layoff=cps_layoff*10	
	
save "clean/us_insecure_summary.dta", replace
	
*Produce figure showing job security over time, plus unemployment rate and labor
*market flows
graph twoway (scatter insecure year, c(l) lwidth(medthick) msize(0) lcolor(gs0) yaxis(1)) (scatter insecurity_hrs year, c(l) lwidth(medthick) msize(0) lcolor(gs4) lpattern(longdash) yaxis(2)) (scatter unrate year, c(l) msize(0) lcolor(gs7) lpattern(dash) lwidth(medthick) yaxis(1)) (scatter cps_layoff year, c(l) lcolor(gs11) msize(0) lpattern(shortdash) lwidth(medthick) yaxis(1)), graphregion(color(white)) bgcolor(white) xtitle("Year") title("US job insecurity, 1978-2018") xlabel(1978(2)2018, angle(45)) scale(.8) ylabel(0(.05).125, axis(1)) ylabel(.1(.05).2, axis(2)) legend(label(1 "% insecure (GSS)") label(2 "Unemployment rate") label(3 "Layoff rate*10") label(4 "Probability of job loss" "(HRS)")) ytitle("Proportion", axis(1)) ytitle("Probability of job loss, HRS", axis(2))

graph export "$usgraphics/insecure_us.png", as(png) name("Graph") replace

////////////////////////////////////////////////////////////////////////////////
*Figure 3
////////////////////////////////////////////////////////////////////////////////

cd "$ukdata"

use "clean/uk_linear_reverse.dta", clear
	drop if inlist(wave,19,21,23,25)
	forval i=1/4 {
		gen ukhls_security`i'=1 if ukhls_security==`i'
		replace ukhls_security`i'=0 if ukhls_security!=.&ukhls_security>=0&ukhls_security!=`i'
		}
		
	forval i=1/4 {
		replace ukhls_security`i'=ukhls`i' if inrange(wave,1,18)&!inlist(wave,6,7)
	}

collapse (mean) ukhls_security1-ukhls_security4 if insample [pw=relative_xw], by(year)

gen insecure=ukhls_security1+ukhls_security2
sort year

*Add unemployment rates
merge 1:1 year using "clean/unrate.dta"
	keep if _merge==3
	drop _merge

*Merge in separation rates
merge 1:1 year using "clean/uk_flows.dta"
	drop if _merge==2
	drop _merge
gen lfs_total = e_u + e_n + e_e
gen lfs_total_invol = e_u*invol_u + e_n*invol_n + e_e*invol_e

keep insecure urate lfs_total_invol year

save "clean/uk_insecure_summary.dta", replace
	
*Produce figure showing job security over time, plus unemployment rate and labor
*market flows	
graph twoway (scatter insecure year, c(l) msize(0) lcolor(gs0) lwidth(medthick)) (scatter urate year, c(l) lpattern(longdash) lcolor(gs7) msize(0) lwidth(medthick)) (scatter lfs_total_invol year, c(l) lpattern(dash) lcolor(gs11) msize(0) lwidth(medthick)), graphregion(color(white)) bgcolor(white) xline(2008.5, lpattern(dash) lcolor(red)) xlabel(1991/2018, angle(45)) title("Job insecurity in the UK, 1991-2018") scale(.9) ytitle("Proportion") legend(label(1 "% insecure") label(2 "Unemployment rate") label(3 "Involuntary separation rate"))

graph export "$ukgraphics/insecurity_overall_uk.png", as(png) name("Graph") replace

////////////////////////////////////////////////////////////////////////////////
*Figure 4
////////////////////////////////////////////////////////////////////////////////

cd "$germanydata"

use "clean/soep_clean.dta", clear

collapse (mean) insecure if insample [pw=xw], by(year)
tempfile main
save `main'

*By region
use "clean/soep_clean.dta", clear

collapse (mean) insecure if insample [pw=xw], by(year region)
	rename insecure insecure_region

merge m:1 year using `main'
	drop _merge

*Add unemployment rate
merge m:1 year using "clean/unemployment.dta"
	keep if _merge==3
	drop _merge
	
*Merge in separation rates
merge m:1 year using "clean/germany_flows.dta"
	drop if _merge==2
	drop _merge
	drop if year==1984

keep insecure insecure_region urate layoff_total region year

save "clean/soep_insecure_summary.dta", replace

graph twoway (scatter insecure year, c(l) lcolor(gs0) msize(0) lwidth(medthick)) (scatter insecure_region year if region==1&year>1991, c(l) lcolor(gs4) lpattern(longdash) msize(0)) (scatter insecure_region year if region==2&year>1991, c(l) lcolor(gs4) lpattern(shortdash) msize(0)) (scatter urate year, c(l) lpattern(dash_dot) lcolor(gs7) msize(0) lwidth(medthick)) (scatter layoff_total year if region==1, c(l) msize(0) lcolor(gs11) lpattern(shortdash) lwidth(medthick)), graphregion(color(white)) bgcolor(white) title("Job insecurity in Germany, 1985-2018") xtitle("Year") ytitle("Proportion of workers") xlabel(1985(2)2018, angle(45)) legend(label(1 "Proportion that feel" "insecure, total") label(2 "Proportion that feel" "insecure, West Germany") label(3 "Proportion that feel" "insecure, East Germany") label(4 "Unemployment rate") label(5 "Layoff rate"))

graph export "$germanygraphics/insecurity_de.png", as(png) name("Graph") replace

////////////////////////////////////////////////////////////////////////////////
*Figure 5
////////////////////////////////////////////////////////////////////////////////

*UK

cd "$ukdata"

use "clean/uk_linear_reverse.dta", clear
collapse (mean) satisfied if insample [pw=relative_xw], by(year)
save "clean/uk_satisfaction_summary.dta", replace

*US

cd "$usdata"

use "clean/gss_clean.dta", clear
	drop if employed==1&satjob>=.
	forval i=1/4 {
		gen satjob`i'=1 if satjob==`i'
		recode satjob`i' .=0
	}

collapse (mean) satjob* if insample==1 [pweight=wtssall], by(year)
	drop satjob
	gen satisfied=satjob1+satjob2
	drop satjob*
save "clean/us_satisfaction_summary.dta", replace

*Germany

cd "$germanydata"

use "clean/soep_clean.dta", clear
	drop if jobsat<0
	gen satisfied=1 if jobsat>=5
	replace satisfied=0 if jobsat<=4

collapse (mean) satisfied if insample [pw=xw], by(year)

save "clean/germany_satisfaction_summary.dta", replace

*Combine satisfaction data for each country

cd "$ukdata"

use "clean/uk_satisfaction_summary.dta", clear
	rename satisfied satisfied_uk
	label var satisfied_uk "UK"
	
cd "$usdata"

merge 1:1 year using "clean/us_satisfaction_summary.dta"
	drop _merge
	rename satisfied satisfied_us
	label var satisfied_us "US"
	
cd "$germanydata"

merge 1:1 year using "clean/germany_satisfaction_summary.dta"
	drop _merge
	rename satisfied satisfied_de
	label var satisfied_de "Germany"

sort year	
	
cd "$othergraphics"

*Plot satisfaction over time for each country
graph twoway (scatter satisfied_us year, c(l) msize(0) lwidth(medthick) lpattern(solid) lcolor(gs0)) (scatter satisfied_uk year, c(l) msize(0) lwidth(medthick) lpattern(longdash) lcolor(gs7)) (scatter satisfied_de year, c(l) msize(0) lwidth(medthick) lpattern(dash) lcolor(gs11)), graphregion(color(white)) bgcolor(white) xlabel(1978(2)2018, angle(45)) xline(2008.5, lcolor(red) lpattern(dash)) yscale(range(.8 .95)) ylabel(.8(.05).95) title("Job satisfaction in the US, UK, and Germany") ytitle("Proportion satisfied with job") xtitle("Year")

graph export "satisfaction.png", as(png) name("Graph") replace
